package com.hkzhao.reportingsystem.controller;

import com.hkzhao.reportingsystem.common.api.CommonPage;
import com.hkzhao.reportingsystem.common.api.CommonResult;
import com.hkzhao.reportingsystem.common.utils.WeixinGetForToken;
import com.hkzhao.reportingsystem.dto.ListStars;
import com.hkzhao.reportingsystem.mbg.model.Comment;
import com.hkzhao.reportingsystem.mbg.model.News;
import com.hkzhao.reportingsystem.service.DoctorService;
import com.hkzhao.reportingsystem.service.NormalUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags = "NormalUserController",description = "普通用户控制器，不加权限控制")
@Controller
@RequestMapping("/normalUser")
public class NormalUserController {
    private static final Logger LOGGER = LoggerFactory.getLogger(NormalUserController.class);
    @Autowired
    private NormalUserService userService;
    @Autowired
    private DoctorService  doctorService;
    @ApiOperation("调用Wechat的token接口返回唯一标识token")
    @ResponseBody
    @RequestMapping(value = "/login",method = RequestMethod.GET)
    public String  login(@RequestParam String code,@RequestParam String username)
    {
        //得到wechat中的code
        String WX_URL = "https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code";
        String requestUrl = WX_URL.replace("APPID","wx2ddc5783ef842ee4").
                replace("SECRET","a2e2f0b303b0a97f211c61b91d8dce23").
                replace("JSCODE", code).replace("authorization_code", "authorization_code");
        String returnvalue = WeixinGetForToken.GET(requestUrl);
        LOGGER.info("Get from Wechat, token is :{}",returnvalue);
        JSONObject converval = JSONObject.fromObject(returnvalue);
        String openid = (String)converval.get("openid");
        String sessionkey = (String) converval.get("session_key");
        LOGGER.info("Wechat user is:{}" ,openid);
        if(userService.userlogin(openid,username))
            LOGGER.info("Wechat user login success:{}" ,openid);
        else
            LOGGER.info("Wechat user login failed:{}" ,openid);
        return openid;
    }
    @ApiOperation("返回分页后的新闻信息 pageNum+pageSize 前端自己弄个计数器看更新请求就好了")
    @ResponseBody
    @RequestMapping(value = "/getNews",method = RequestMethod.GET)
    public CommonResult<CommonPage<News>> getNews(@RequestParam(value = "pageNum") @ApiParam("页码") int pageNum,
                                                  @RequestParam(value = "pageSize") @ApiParam("页面大小") int pageSize)
    {
            List<News> list = userService.getListNews(pageNum,pageSize);
            return CommonResult.success(CommonPage.restPage(list));
    }
    @ApiOperation("返回通过news_id查找的评论信息列表")
    @ResponseBody
    @RequestMapping(value = "/getCommentByNews",method = RequestMethod.GET)
    public CommonResult<CommonPage<Comment>> getCommentByNews(@RequestParam(value = "pageNum") @ApiParam("页码") int pageNum,
                                                             @RequestParam(value = "pageSize") @ApiParam("页面大小") int pageSize,
                                                             @RequestParam(value = "news_id") @ApiParam("新闻的token") String news_id){

        List<Comment> list = userService.getCommentListByNews(pageNum, pageSize, news_id);
        return CommonResult.success(CommonPage.restPage(list));
    }
    @ApiOperation("返回通过user_token查找的历史评论")
    @ResponseBody
    @RequestMapping(value = "/getCommentByUser",method = RequestMethod.GET)
    public CommonResult<CommonPage<Comment>> getCommentByUser(@RequestParam(value = "pageNum") @ApiParam("页码") int pageNum,
                                                              @RequestParam(value = "pageSize") @ApiParam("页面大小") int pageSize,
                                                              @RequestParam(value = "user_token") @ApiParam("微信请求的唯一标识token") String user_token)
    {    if(user_token.length()==0)
        return CommonResult.failed("usertoken为空");
        List<Comment> list = userService.getCommentListByUser(pageNum, pageSize, user_token);
        return CommonResult.success(CommonPage.restPage(list));
    }
    @ApiOperation("通过返回user_token和评论id删除评论")
    @ResponseBody
    @RequestMapping(value = "/deleteCommentByUser",method = RequestMethod.GET)
    public CommonResult deleteCommentByUser(@RequestParam("Comment_id") @ApiParam("评论的唯一id")String comment_id,
                                            @RequestParam("user_token") @ApiParam("用户唯一标识token")String user_token)
    {
        return userService.deleteCommentByUser(comment_id, user_token);
    }
    @ApiOperation("通过返回user_token和news_id以及评论，添加评论")
    @ResponseBody
    @RequestMapping(value = "/addCommentByUser",method = RequestMethod.GET)
    public CommonResult addCommentByUser(@RequestParam("user_token") @ApiParam("用户的token")String token,
                                         @RequestParam("news_id") @ApiParam("新闻的id")String news_id,
                                         @RequestParam("text") @ApiParam("评论") String text){
        return userService.addCommentByUserAndNew(token, news_id, text);
    }
    @ApiOperation("点赞的api")
    @ResponseBody
    @RequestMapping(value = "/addStars",method = RequestMethod.POST)
    public CommonResult addStars(@RequestBody ListStars l)
    {
           return userService.addStarsToComment(l.getComment_id(),l.getUser_token());
    }
    @ApiOperation("取消点赞")
    @ResponseBody
    @RequestMapping(value = "removeStars",method = RequestMethod.POST)
    public CommonResult reomveStars(@RequestBody ListStars l)
    {

        return userService.cancelStarsToComment(l.getComment_id(),l.getUser_token());
    }
    @ApiOperation("年龄段数据")
    @ResponseBody
    @RequestMapping(value = "/getAgeData",method = RequestMethod.GET)
    public CommonResult getAgeData()
    {
        return userService.getOrderByAge();
    }
    @ApiOperation("性别数据")
    @ResponseBody
    @RequestMapping(value = "/getGenderData",method = RequestMethod.GET)
    public CommonResult getGenderData()
    {
        return userService.getOrderByGender();
    }
    @ApiOperation("危重症数据")
    @ResponseBody
    @RequestMapping(value = "/getLevelData",method = RequestMethod.GET)
    public CommonResult getLevelData()
    {
        return userService.getOrderByLevel();
    }
    @ApiOperation("省的数据 我分开了两个接口 就不封装成跟市一起了，前端根据省的名字可以找到所有的city 直接循环就完事了")
    @ResponseBody
    @RequestMapping(value = "/getProvinceData",method = RequestMethod.GET)
    public CommonResult getProvinceData()
    {
        return userService.getProvinceData();
    }
    @ApiOperation("市的数据 如图 直辖市当成了省 直辖市的区就是市")
    @ResponseBody
    @RequestMapping(value = "/getCityData",method = RequestMethod.GET)
    public CommonResult getCityData()
    {
        return userService.getCityData();
    }
    @ApiOperation("折线图新增数据")
    @ResponseBody
    @RequestMapping(value = "/getAddtionData",method = RequestMethod.GET)
    public CommonResult getAddtionData()
    {
        return userService.getAddtionEveryDay();
    }
    @ApiOperation("添加时间轴的所有数据")
    @ResponseBody
    @RequestMapping(value = "/getTotalData",method = RequestMethod.GET)
    public CommonResult getTotalData()
    {
        return userService.getTotalEveryDay();
    }
}
